<?php

namespace App\Http\Controllers\Income;

use App\Http\Controllers\Controller;
use App\Models\Income\IncomeCharteredCarFixed;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class IncomeCharteredCarFixedController extends Controller
{
    public function incomeCharteredCarFixedList(Request $request)
    {
        $params = $countParams = $request->all();
        unset($countParams['pageNumber'], $countParams['pageSize']);

        $data = $this->getIncomeCharteredCarFixedList($params)
            ->orderByDesc('created_at')
            ->get()
            ->toArray();
        $dataCount = $this->getIncomeCharteredCarFixedList($countParams)->count();

        return response()->formatJson(['charteredCarFixedRecord' => $data, 'count' => $dataCount]);
    }

    public function getIncomeCharteredCarFixedList($params): \Illuminate\Database\Eloquent\Builder
    {
        $query = IncomeCharteredCarFixed::query();

        if (isset($params['pageNumber'], $params['pageSize'])) {
            $query
                ->limit($params['pageSize'])
                ->offset(($params['pageNumber'] - 1) * $params['pageSize']);
        }

        return $query;
    }

    /**
     * 创建车辆信息
     *
     * @param  Request  $request
     *
     * @return mixed
     */
    public function storeCharteredCarFixed(Request $request)
    {
        $params = $request->all();
        $params['date'] .= '-01';
        $end_date = date("Y-m-t", strtotime($params['date'], time()));
        $charteredCarFixed = new IncomeCharteredCarFixed($params);
        $result = $charteredCarFixed->save();
        $data = [
            [
                'license_plate_num' => '21061', 'date_start_at' => $params['date'], 'date_end_at' => $end_date,
                'chartered_income'  => $params['chartered_income'] / 4,
            ],
            [
                'license_plate_num' => '21901', 'date_start_at' => $params['date'], 'date_end_at' => $end_date,
                'chartered_income'  => $params['chartered_income'] / 4,
            ],
            [
                'license_plate_num' => '21023', 'date_start_at' => $params['date'], 'date_end_at' => $end_date,
                'chartered_income'  => $params['chartered_income'] / 4,
            ],
            [
                'license_plate_num' => '21905', 'date_start_at' => $params['date'], 'date_end_at' => $end_date,
                'chartered_income'  => $params['chartered_income'] / 4,
            ],
        ];
        DB::table('akbus_income_chartered_statistics')->insert($data);
        $message = $result ? '添加成功' : '添加失败';

        return response()->formatJson($result, $message);
    }

    /**
     * 修改文章
     *
     * @param  Request  $request
     * @param           $id
     *
     * @return mixed
     */
    public function updateCharteredCarFixed(Request $request, $id)
    {
        $params = $request->all();
        $params['date'] .= '-01';
        $end_date = date("Y-m-t", strtotime($params['date'], time()));
        $charteredCarFixed = IncomeCharteredCarFixed::query()->find($id);
        $result = $charteredCarFixed->update($params);
        DB::table('akbus_income_chartered_statistics')
            ->whereIn('license_plate_num', ['21061', '21901', '21023', '21905'])
            ->where('date_start_at', $params['date'])
            ->delete();
        $data = [
            [
                'license_plate_num' => '21061', 'date_start_at' => $params['date'], 'date_end_at' => $end_date,
                'chartered_income'  => $params['chartered_income'] / 4,
            ],
            [
                'license_plate_num' => '21901', 'date_start_at' => $params['date'], 'date_end_at' => $end_date,
                'chartered_income'  => $params['chartered_income'] / 4,
            ],
            [
                'license_plate_num' => '21023', 'date_start_at' => $params['date'], 'date_end_at' => $end_date,
                'chartered_income'  => $params['chartered_income'] / 4,
            ],
            [
                'license_plate_num' => '21905', 'date_start_at' => $params['date'], 'date_end_at' => $end_date,
                'chartered_income'  => $params['chartered_income'] / 4,
            ],
        ];
        DB::table('akbus_income_chartered_statistics')->insert($data);

        $message = $result ? '修改成功' : '修改失败';

        return response()->formatJson($result, $message);
    }

    /**
     * 删除车辆信息
     *
     * @param  Request  $request
     *
     * @return mixed
     */
    public function destroyCharteredCarFixed(Request $request)
    {
        $result = false;
        if ($request->has('ids')) {
            $ids = explode(',', $request['ids']);
            $charteredCarFixedCount = IncomeCharteredCarFixed::destroy($ids);
            $result = $charteredCarFixedCount !== 0;
        }

        $message = $result ? '删除成功' : '删除失败';

        return response()->formatJson($result, $message);
    }
}
